package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.BounceInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.model.impl.UserImpl;
import com.wanglibao.woodtrade.woodtrade.ui.activity.user.LoginActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
/*
*
* 发布信息
* */
public class ReleaseActivity extends Activity implements View.OnClickListener{

    @Bind(R.id.imgv_popup_buy)
    ImageView mImgvPopupBuy;
    @Bind(R.id.imgv_popup_sell)
    ImageView mImgvPopupSell;
    @Bind(R.id.imgv_popup_close)
    ImageView mImgvPopupClose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_release);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        mImgvPopupBuy.setOnClickListener(this);
        mImgvPopupSell.setOnClickListener(this);
        mImgvPopupClose.setOnClickListener(this);
        setShowAnimation(mImgvPopupBuy,1000);
        setShowAnimation(mImgvPopupSell,1000);
        setShowAnimation(mImgvPopupClose,1000);
    }

    /**
     * View渐现动画效果
     *
     */
    private void setShowAnimation(View view, int duration ){
        if( null == view || duration < 0 ){
            return;
        }
        AlphaAnimation mShowAnimation = new AlphaAnimation(0.0f, 1.0f);
//        mShowAnimation.setInterpolator(new AccelerateInterpolator());
        Animation scaleAnimation = new ScaleAnimation(0.1f, 1.0f,0.1f,1.0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        AnimationSet set=new AnimationSet(true);
        set.setInterpolator(new BounceInterpolator());
        set.addAnimation(mShowAnimation);
        set.addAnimation(scaleAnimation);
        set.setDuration(duration);
        view.startAnimation(set);
    }
    private AlphaAnimation mHideAnimation= null;
    /**
     * View渐隐动画效果
     *
     */
    private void setHideAnimation(View view,int duration ){
        if(duration < 0 ){
            return;
        }
        if( null != mHideAnimation ){
            mHideAnimation.cancel( );
        }
        mHideAnimation = new AlphaAnimation(1.0f, 0.0f);
        mHideAnimation.setDuration( duration );
        mHideAnimation.setFillAfter( true );
        view.startAnimation(mHideAnimation);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this,ReleaseInfoActivity.class);
        boolean isLogin = false;
        switch(v.getId()){
            case R.id.imgv_popup_buy:
                isLogin = new UserImpl(this).isLogin();
                if (isLogin){
                    intent.putExtra("type",ReleaseInfoActivity.TYPE_DEMAND);
                    startActivityForResult(intent,1);
                    finish();
                }else{
                    startActivity(new Intent(this, LoginActivity.class));
                }

                break;
            case R.id.imgv_popup_sell:
                isLogin = new UserImpl(this).isLogin();
                if (isLogin){
                    intent.putExtra("type",ReleaseInfoActivity.TYPE_SUPPLY);
                    startActivityForResult(intent,0);
                    finish();
                }else {
                    startActivity(new Intent(this, LoginActivity.class));
                }

                break;
            case R.id.imgv_popup_close:
                finish();

                break;
        }

    }

}
